local jianying = fk.CreateSkill{
    name = "gai__jianying",
  }
  
  Fk:loadTranslationTable{
    ["gai__jianying"] = "渐营",
    ["#gai__jianying_trigger"] = "渐营",
    [":gai__jianying"] = "当你使用牌时，若此牌与你使用的上一张牌点数或花色相同，你可以摸一张牌。"..
      "出牌阶段限一次，你可以将一张牌当任意一种基本牌使用，若你于此阶段内使用的上一张牌有花色，则此牌花色视为你本回合使用的上一张牌的花色。",
  
    ["#gai__jianying-active"] = "渐营：将一张牌转化为任意基本牌使用",
    ["@gai__jianying_record"] = "渐营",
  
    ["$gai__jianying1"] = "主公坐占胜机，缓行亦无所碍。",
    ["$gai__jianying2"] = "得胜已是定局，何必急功近利？",
  }
  
  jianying:addEffect("viewas", {
    max_phase_use_time = 1,
    prompt = "#gai__jianying-active",
    interaction = function(self, player)
      local all_names = Fk:getAllCardNames("b")
      return UI.CardNameBox{
        choices = player:getViewAsCardNames(jianying.name, all_names),
        all_choices = all_names,
      }
    end,
    handly_pile = true,
    filter_pattern = {
      min_num = 1,
      max_num = 1,
      pattern = ".",
    },
    view_as = function(self, player, cards)
      if Fk.all_card_types[self.interaction.data] == nil or #cards ~= 1 then return end
      local card = Fk:cloneCard(self.interaction.data)
      card:addSubcards(cards)
  
      local suitstrings = {"spade", "heart", "club", "diamond"}
      local suits = {Card.Spade, Card.Heart, Card.Club, Card.Diamond}
      local colors = {Card.Black, Card.Red, Card.Black, Card.Diamond}
      local suit = player:getMark("gai__jianying_suit-phase")
      if table.contains(suitstrings, suit) then
        card.suit = suits[table.indexOf(suitstrings, suit)]
        card.color = colors[table.indexOf(suitstrings, suit)]
      end
  
      card.skillName = self.name
      return card
    end,
    before_use = function(self, player, use)
      player.room:setPlayerMark(player, "gai__jianying_used-phase", 1)
    end,
    enabled_at_play = function(self, player)
      return player:getMark("gai__jianying_used-phase") == 0
    end,
  })
  
  jianying:addEffect(fk.CardUsing, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
      return data.from == player and (data.extra_data or {}).gai_jianying_triggerable and player:hasSkill(jianying.name)
    end,
    on_use = function(self, event, target, player, data)
      player.room:notifySkillInvoked(player, jianying.name)
      player:drawCards(1, jianying.name)
    end,
  })
  
  jianying:addEffect(fk.AfterCardUseDeclared, {
    can_refresh = function(self, event, target, player, data)
      return target == player and player:hasSkill(jianying.name, true)
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      local card = data.card
      
      local last_used = player:getMark("@gai__jianying_record")
      if type(last_used) == "table" and #last_used == 2 then
        local last_suit, last_number = last_used[1], last_used[2]
        local current_suit = card:getSuitString(true)
        local current_number = card:getNumberStr()
        
        if last_suit == current_suit or last_number == current_number then
          data.extra_data = data.extra_data or {}
          data.extra_data.gai_jianying_triggerable = true
        end
      end
      
      if player.phase == Player.Play then
        if card.suit == Card.NoSuit then
          room:setPlayerMark(player, "gai__jianying_suit-phase", 0)
        else
          room:setPlayerMark(player, "gai__jianying_suit-phase", card:getSuitString())
        end
        room:setPlayerMark(player, "gai__jianying_number-phase", card.number)
      end
      
      room:setPlayerMark(player, "@gai__jianying_record", {card:getSuitString(true), card:getNumberStr()})
    end,
  })
  
  jianying:addLoseEffect(function(self, player)
    local room = player.room
    room:setPlayerMark(player, "gai__jianying_used-phase", 0)
    room:setPlayerMark(player, "gai__jianying_suit-phase", 0)
    room:setPlayerMark(player, "gai__jianying_number-phase", 0)
    room:setPlayerMark(player, "@gai__jianying_record", 0)
  end)
  
  return jianying